package omm.services.imp;

import java.util.List;

import omm.dao.OrderDAO;
import omm.dao.QuotationDAO;
import omm.dao.imp.QuotationAndOrderStrategy;
import omm.dao.imp.QuotationReportsStrategy;
import omm.domain.Graphic;
import omm.domain.Quotation;
import omm.domain.comparativegraph.ComparativeGraphic;
import omm.domain.comparativegraph.ComparativeGraphicFactory;
import omm.domain.enums.Commission;
import omm.domain.enums.GraphicType;
import omm.domain.enums.GroupBy;
import omm.domain.evolutiongraph.AnualByMonthPeriodStrategy;
import omm.domain.evolutiongraph.CalendarAnualByMonthPeriodStrategy;
import omm.domain.evolutiongraph.EvolutionGraphic;
import omm.domain.evolutiongraph.EvolutionGraphicFactory;
import omm.domain.evolutiongraph.LastClosedMonthPeriodStrategy;
import omm.domain.evolutiongraph.PeriodStrategy;
import omm.domain.evolutiongraph.EvolutionGraphicFactory.Period;
import omm.domain.evolutiongraph.EvolutionGraphicFactory.Unit;
import omm.domain.generalgraph.MonetaryGraphic;
import omm.domain.generalgraph.QuantityGraphic;
import omm.services.QuotationServices;
import omm.util.Graphics;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QuotationServicesImp implements QuotationServices{
	
	QuotationDAO quotationDAO;
	OrderDAO orderDAO;
	
	@Autowired
	public QuotationServicesImp(QuotationDAO quotationDAO, OrderDAO orderDAO){
		this.quotationDAO = quotationDAO; 
		this.orderDAO = orderDAO;
	}
	
	@Override
	public List<Quotation> getBusinessFilterQuotations(DateTime beginning, DateTime ending) {
		return quotationDAO.getAllWithFilter(new QuotationReportsStrategy(),beginning,ending);
	}

	@Override
	public MonetaryGraphic getMonetaryGraphic(List<Quotation> quotations, DateTime beginning, DateTime ending) {
		return new MonetaryGraphic(quotations,beginning,ending);
	}

	@Override
	public QuantityGraphic getQuantityGraphic(List<Quotation> quotations, DateTime beginning, DateTime ending) {
		 return new QuantityGraphic(quotations,beginning,ending);
	}
		
	@Override
	public EvolutionGraphic getEvolutionGraphic(List<Quotation> quotations, Integer period, Integer commission, Integer unit,DateTime beginning, DateTime ending) {
		Period periodState = Period.fromCode(period);
		Commission commisionState = Commission.fromCode(commission);
		Unit unitState = Unit.fromCode(unit);
		EvolutionGraphicFactory factory = new EvolutionGraphicFactory();
		return factory.createEvolutionGraphic(quotations, periodState, commisionState, unitState,ending);
	}

	@Override
	public String getComparativeGraphic(Integer groupby, Integer commission, Integer graphic) {
		GroupBy groupbyState = GroupBy.fromCode(groupby);
		Commission commissionState = Commission.fromCode(commission);
		GraphicType graphicState = GraphicType.fromCode(graphic);
		ComparativeGraphicFactory factory = new ComparativeGraphicFactory();
		ComparativeGraphic comparativeGraphic = factory.createComparativeGraphic(quotationDAO, orderDAO,graphicState, commissionState,groupbyState);
		return comparativeGraphic.getGraphicURL();
	}
	
}
